package com.hspedu.seckill.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: xujiabing
 * @date: 2024-12-18 20:48
 * @description: rabbitmq秒杀配置类
 */
@Configuration
public class RabbitMQSecKillConfig {

    /**
     * 队列
     */
    public static final String QUEUE = "seckillQueue";

    /**
     * 交换机
     */
    public static final String EXCHANGE = "seckillExchange";

    /**
     * topic路由匹配 # 0个或多个
     */
    private static final String TOPIC_ROUTE = "seckill.#";

    /**
     * 创建队列
     * @return
     */
    @Bean
    public Queue queue_seckill() {
        return new Queue(QUEUE);
    }

    /**
     * 创建主题模式交换机
     * @return
     */
    @Bean
    public TopicExchange topicExchange_seckill() {
        return new TopicExchange(EXCHANGE);
    }

    /**
     * 绑定交换机和队列, 并指定路由匹配
     * @return
     */
    @Bean
    public Binding binding_seckill() {
        return BindingBuilder.bind(queue_seckill())
                .to(topicExchange_seckill()).with(TOPIC_ROUTE);
    }

}
